package com.pisolution.scheduler.datasource.command;

import com.pisolution.scheduler.datasource.instance.FailureStrategy;
import com.pisolution.scheduler.datasource.instance.Priority;
import com.pisolution.scheduler.datasource.instance.WarningType;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.util.Date;

/**
 * 执行失败的控制指令数据实体.
 *
 * @author sora
 * @version 0.1.0
 */
@TableName(value = "error_command")
public class ErrorCommand {
    // -------------------------------------------------------------------------------------------------
    //  数据单元 (基础信息)
    // -------------------------------------------------------------------------------------------------

    /** 指令标识. */
    @TableId(value = "id", type = IdType.INPUT)
    private Integer id;

    /** 指令类型. */
    @TableField(value = "type")
    private CommandType type;

    /** 指令参数. */
    @TableField(value = "params")
    private String params;

    /** 执行失败的原因. */
    @TableField(value = "cause")
    private String cause;

    /** 处理时间. */
    @TableField(value = "start_time")
    private Date startTime;

    /** 更新时间. */
    @TableField(value = "update_time")
    private Date updateTime;

    // -------------------------------------------------------------------------------------------------
    //  数据单元 (关联信息)
    // -------------------------------------------------------------------------------------------------

    /** 工作流标识. */
    @TableField(value = "workflow_id")
    private int workflowId;

    /** 工作流实例优先级. */
    @TableField(value = "priority")
    private Priority priority;

    /** 运行工作流实例的工作节点分组. */
    @TableField(value = "worker_group")
    private String workerGroup;

    /** 工作流实例运行失败时的策略. */
    @TableField(value = "strategy")
    private FailureStrategy strategy;

    /** 工作流实例运行期告警类型. */
    @TableField(value = "warning_type")
    private WarningType warningType;

    /** 工作流实例告警组标识. */
    @TableField(value = "warning_group_id")
    private Integer warningGroupId;

    /** 工作流实例调度时间. */
    @TableField(value = "schedule_time")
    private Date scheduleTime;

    // -------------------------------------------------------------------------------------------------
    //  初始化
    // -------------------------------------------------------------------------------------------------

    /**
     * 以给定的控制指令与失败原因初始化该数据实体.
     *
     * @param command 原始控制指令.
     * @param cause 执行失败的原因.
     */
    public ErrorCommand(final Command command, final String cause) {
        this.id = command.getId();
        this.type = command.getType();
        this.params = command.getParams();
        this.cause = cause;
        this.startTime = command.getStartTime();
        this.updateTime = command.getUpdateTime();
        this.workflowId = command.getWorkflowId();
        this.priority = command.getPriority();
        this.strategy = command.getStrategy();
        this.warningType = command.getWarningType();
        this.warningGroupId = command.getWarningGroupId();
        this.scheduleTime = command.getScheduleTime();
    }
}
